﻿using App.Model;
using App.Untiys;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Win.Dal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace App.Desk.Sys.Depart
{
    public partial class FrmEdit : UIEditForm
    {
        private bool CheckDropEmpty(UIIntegerUpDown uIIntegerUpDown, string retMsg)
        {
            if (uIIntegerUpDown.Value <= 0)
            {
                UIMessageBox.ShowWarning(retMsg);
                return false;
            }
            return true;
        }

        private ProDepart postData;

        public ProDepart PostData
        {
            get
            {
                if (postData == null)
                {
                    postData = new ProDepart();
                }
                postData.Id = txtId.Text.Trim().ToInt();
                postData.DepartName = txtDepartName.Text.Trim();
                postData.Disable = txtDisable.Active ? 1 : 0;
                return postData;
            }
            set
            {
                postData = value;
                txtId.Text = value.Id.ToStringEx();
                txtDepartName.Text = value.DepartName;
                //depart
                txtDisable.Active = value.Disable == 1;
            }
        }
        public FrmEdit(int id = 0)
        {
            InitializeComponent();
            if (id <= 0)
                Text = "新增";
            else
            {
                Text = "修改";
                PostData = ProDepartServices.Info(new ProDepart { Id = id });
            }
        }

        private void FrmEdit_ButtonOkClick(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtDepartName.Text.Trim()))
            {
                txtDepartName.Focus();
                UIMessageTip.ShowWarning("请输入名称！"); return;
            }
            if (ProDepartServices.Isrepeat(PostData.Id, txtDepartName.Text.Trim()))
            {
                txtDepartName.Focus();
                UIMessageTip.ShowWarning("部门名称重复，请重新输入！"); return;
            }
            if (string.IsNullOrEmpty(txtDepartName.Text.Trim()))
            {
                txtDepartName.Focus();
                UIMessageTip.ShowWarning("请输入名称！"); return;
            }
            PostData.AddUser = Config.CurrentUser.Id;
            if (ProDepartServices.Edit(PostData))
            {
                UIMessageBox.ShowSuccess("保存成功！");
                this.Close();
            }
            else
                UIMessageBox.ShowError("保存失败！");
        }
    }
}
